package com.gxa.community.admin.controller.goods;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.community.admin.mapper.goods.GoodsVoMapper;
import com.gxa.community.admin.service.goods.impl.GoodsClassServiceImpl;
import com.gxa.community.core.pojo.domain.goodsdo.GoodsClass;
import com.gxa.community.core.pojo.dto.goods.GoodsInfoDto;
import com.gxa.community.core.pojo.vo.goodsvo.GoodsInfoVo;
import com.gxa.community.core.pojo.vo.goodsvo.GoodsVo;
import com.gxa.community.core.utils.R;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author gxa
 * @since 2022-07-18
 */
@Validated
@RestController
@RequestMapping("/goods/class")
@Slf4j
public class GoodsClassController {
    @Autowired
    GoodsClassServiceImpl goodsClassService;
    @Autowired
    GoodsVoMapper goodsVoMapper;
    /**
     * 添加分类
     * @param goodsClass
     * @return
     */
    @PostMapping("/add")
    public R addClass(@RequestBody GoodsClass goodsClass) {
        log.info("{}",goodsClass);
        boolean b = goodsClassService.saveOrUpdate(goodsClass);
        if (b){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 查询所有分类
     * @return
     */
    @GetMapping("/query")
    public R queryClass(@NotNull(message = "page不能为空") Integer page,
                        @NotNull(message = "size不能为空") Integer size) {
        Page<GoodsClass> Page2 = new Page<>(page,size);
        var page1 = goodsClassService.page(Page2, null);
        return R.ok().put("data",page1.getRecords()).put("cont",page1.getTotal());
    }


    /**
     * 修改分类
     * @param goodsClass
     * @return
     */
    @PutMapping("/edit")
    public R editClass(@RequestBody GoodsClass goodsClass) {
        boolean b = goodsClassService.updateById(goodsClass);
        if (b){
            return R.ok();
        }
        return R.error();
    }



    /**
     * 批量删除
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    public R del(@NotNull(message = "id不能为空") Integer... id) {
        Integer count = 0;
        for (Integer integer : id) {
            boolean b = goodsClassService.removeById(integer);
            if (b){
                count++;
            }
        }
        return R.ok("成功删除了:"+count+"个");
    }

    @GetMapping("/seek")
    public R seek(@NotEmpty(message = "搜索内容不能为空") String name,
                  @NotNull(message = "page不能为空") Integer page,
                  @NotNull(message = "size不能为空") Integer size) {
        QueryWrapper<GoodsClass> wrapper1 = new QueryWrapper<GoodsClass>();
        wrapper1.likeRight("name",name);
        Page<GoodsClass> Page = new Page<>(page,size);
        var page1 = goodsClassService.page(Page, wrapper1);
        //把分页完成的数据和总共的数量返回
        return R.ok().put("data",page1.getRecords()).put("cont",page1.getTotal());
    }

    /**
     * 远程通信接口
     * 获取所有未被禁用的分类
     * @return
     */
    @GetMapping("/queryClassList")
    public  List<GoodsInfoDto> queryClasss(){
        List<GoodsInfoDto> goodsInfoDtos = new ArrayList<>();
        QueryWrapper<GoodsClass> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        List<GoodsClass> list = goodsClassService.list(wrapper);
        for (GoodsClass goodsClass : list) {
            System.out.println("goodsClass = " + goodsClass);
        }
        for (GoodsClass goodsClass : list) {
            GoodsInfoDto goodsInfoDto = new GoodsInfoDto();
            QueryWrapper<GoodsVo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("classs_id", goodsClass.getId());
            queryWrapper.eq("status",1);
            List<GoodsVo> infoVos = goodsVoMapper.selectList(queryWrapper);
            goodsInfoDto.setGoodsInfoVo(infoVos);
            goodsInfoDto.setName(goodsClass.getName());
            goodsInfoDto.setIndexs(goodsClass.getIndexs());
            goodsInfoDto.setPic(goodsClass.getPic());
            goodsInfoDtos.add(goodsInfoDto);
        }
        return goodsInfoDtos;
    }
}

